import request from "@/lib/request";

// 获取用户图片
export async function getUserImages(params: {
  userId: string;
  page?: number;
  size?: number;
  sortBy?: string;
  sortDir?: string;
}) {
  return request({
    url: "/images/user",
    method: "GET",
    params,
  });
}

// 按使用次数排序
export async function getImagesSortedByUsage(params: {
  page?: number;
  size?: number;
  sortDir?: string;
}) {
  return request({
    url: "/images/sorted/usage",
    method: "GET",
    params,
  });
}

// 按时间排序
export async function getImagesSortedByTime(params: {
  page?: number;
  size?: number;
  timeField?: string;
  sortDir?: string;
}) {
  return request({
    url: "/images/sorted/time",
    method: "GET",
    params,
  });
}

// 分页获取所有图片
export async function getAllImages(params: {
  page: number;
  size: number;
  sortBy: string;
  sortDir: string;
  userId?: string;
}) {
  return request({
    url: "/images",
    method: "GET",
    params,
    headers: params.userId ? { User: params.userId } : undefined,
  });
}

// 根据ID获取图片
export async function getImageById(id: number, userId?: string) {
  return request({
    url: `/images/${id}`,
    method: "GET",
    params: userId ? { userId } : undefined,
    headers: userId ? { User: userId } : undefined,
  });
}

// 搜索图片
export async function searchImages(params: {
  keyword: string;
  page: number;
  size: number;
  sortBy: string;
  sortDir: string;
  userId?: string;
}) {
  return request({
    url: "/images/search",
    method: "GET",
    params,
    headers: params.userId ? { User: params.userId } : undefined,
  });
}

// 根据标签筛选图片
export async function getImagesByTag(tagName: string, userId?: string) {
  return request({
    url: `/images/tag/${tagName}`,
    method: "GET",
    headers: userId ? { User: userId } : undefined,
  });
}

// 根据多个标签筛选图片
export async function getImagesByTags(tags: string[], userId?: string) {
  return request({
    url: "/images/tags",
    method: "GET",
    params: { tags },
    headers: userId ? { User: userId } : undefined,
  });
}

// 根据类型筛选图片
export async function getImagesByType(mimeType: string, userId?: string) {
  return request({
    url: `/images/type/${mimeType}`,
    method: "GET",
    headers: userId ? { User: userId } : undefined,
  });
}

// 根据时间范围筛选图片
export async function getImagesByDateRange(params: {
  startTime: string;
  endTime: string;
  userId?: string;
}) {
  return request({
    url: "/images/date-range",
    method: "GET",
    params,
    headers: params.userId ? { User: params.userId } : undefined,
  });
}

// 获取热门图片
export async function getPopularImages(params: {
  page: number;
  size: number;
  userId?: string;
}) {
  return request({
    url: "/images/popular",
    method: "GET",
    params,
    headers: params.userId ? { User: params.userId } : undefined,
  });
}

// 更新图片信息
export async function updateImage(id: number, data: any, userId?: string) {
  return request({
    url: `/images/${id}/update`,
    method: "POST",
    data,
    headers: userId ? { User: userId } : undefined,
  });
}

// 删除图片
export async function deleteImage(id: number, userId?: string) {
  return request({
    url: `/images/${id}/delete`,
    method: "POST",
    headers: userId ? { User: userId } : undefined,
  });
}

// 批量删除图片
export async function batchDeleteImages(ids: number[], userId?: string) {
  return request({
    url: "/images/batch-delete",
    method: "POST",
    data: ids,
    headers: userId ? { User: userId } : undefined,
  });
}

// 获取图片统计信息
export async function getImageStatistics(userId?: string) {
  return request({
    url: "/images/statistics",
    method: "GET",
    headers: userId ? { User: userId } : undefined,
  });
}

// 获取热门标签
export async function getPopularTags(limit: number, userId?: string) {
  return request({
    url: "/images/popular-tags",
    method: "GET",
    params: { limit },
    headers: userId ? { User: userId } : undefined,
  });
}
